package com.example.demo2.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo2.entity.AddressCity;
import com.example.demo2.entity.AddressCounty;
import com.example.demo2.entity.AddressProvince;
import com.example.demo2.entity.DTO.AddressDTO;
import com.example.demo2.mapper.AddressProvinceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressProvinceService extends ServiceImpl<AddressProvinceMapper, AddressProvince> {



    @Autowired
    private AddressCityService addressCityService;
    @Autowired
    private AddressCountyService addressCountyService;

    public Map<String, Object> findPageOfCustomer(AddressDTO addressDTO) {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<AddressProvince> lqw = new LambdaQueryWrapper<>();
        Page<AddressProvince> iPage = new Page<>(addressDTO.getPage(), addressDTO.getLimit());
        Page<AddressProvince> page = page(iPage, lqw);
        page.getRecords().forEach(item -> {
            LambdaQueryWrapper<AddressCity> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(AddressCity::getProvinceCode, item.getCode());
            List<AddressCity> cityList = addressCityService.list(lqw2);
            item.setCitysList(cityList);
            item.getCitysList().forEach(item2 -> {
                LambdaQueryWrapper<AddressCounty> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(AddressCounty::getCityCode, item2.getCode());
                List<AddressCounty> countyslist = addressCountyService.list(lqw3);
                item2.setCountysList(countyslist);
            });
        });
        map.put("address",page);
        return map;
    }

}
